package com.it.common.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.Log
import android.util.SparseArray
import android.util.SparseBooleanArray
import android.util.SparseIntArray
import android.util.SparseLongArray
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import java.lang.reflect.Array

/**
 *  @Description：底层扩展
 *  @author：weishixiong
 *  @date：2023/4/6 9:52
 *
 */
fun <T> T.isNotEmpty(): Boolean {
    if (this == null) {
        return false
    }
    if (this is String && this.toString().isEmpty()) {
        return false
    }
    if (this.javaClass.isArray && Array.getLength(this) == 0) {
        return false
    }
    if (this is Collection<*> && this.isEmpty()) {
        return false
    }
    if (this is Map<*, *> && this.isEmpty()) {
        return false
    }
    if (this is SparseArray<*> && this.size() == 0) {
        return false
    }
    if (this is SparseBooleanArray && this.size() == 0) {
        return false
    }
    if (this is SparseIntArray && this.size() == 0) {
        return false
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (this is SparseLongArray && this.size() == 0) {
            return false
        }
    }
    return true
}

private var lastClickTime: Long = 0
private var debounceDuration: Long = 200L // 毫秒

//fun View.setOnClickListener(duration: Long = debounceDuration, action: () -> Unit) {
//    setOnClickListener {
//        action()
//    }
//
//}

@SuppressLint("ClickableViewAccessibility")
fun Activity.hideKeyboardOnOutsideTouch() {
    val view: View = findViewById(android.R.id.content)
    view.setOnTouchListener { _, event ->
        if (event.action == MotionEvent.ACTION_DOWN) {
            Log.d("ExtUtils.kt", "hideKeyboardOnOutsideTouch: ")
            val inputMethodManager =
                getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
        }
        false
    }
}

fun View.setOnClick5(callback: () -> Unit) {
    val maxClicks = 5
    var clickCount = 0
    var lastClickTime = 0L

    setOnClickListener {
        val now = System.currentTimeMillis()
        if (now - lastClickTime < 1000) {
            clickCount++
            if (clickCount >= maxClicks) {
                clickCount = 0
                callback()
            }
        } else {
            clickCount = 1
        }
        lastClickTime = now
    }


}

fun String.convertCase(): String {
    val sb = StringBuilder()
    for (i in 0 until this.length) { // 遍历每个字符
        val c = this[i]
        if (c.isUpperCase()) {
            sb.append(c.toLowerCase()) // 大写转小写
        } else if (c.isLowerCase()) {
            sb.append(c.toUpperCase()) // 小写转大写
        } else {
            sb.append(c) // 非字母字符（如数字、标点符号）不变
        }
    }
    return sb.toString()
}

fun String.isValidIP(): Boolean {
    val ipPattern =
        "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$".toRegex()
    return ipPattern.matches(this)
}

fun String.isValidSubnetMask(): Boolean {
    val subnetMaskPattern =
        "^(((0|128|192|224|240|248|252|254|255)\\.){3}(0|128|192|224|240|248|252|254|255))$".toRegex()
    return subnetMaskPattern.matches(this)
}

fun String.isValidGateway(): Boolean {
    val gatewayPattern =
        "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$".toRegex()
    return gatewayPattern.matches(this)
}



